/* 
 * File:   ServerConfFileManager.cpp
 * Author: nahuel
 * 
 * Created on May 23, 2013, 9:53 PM
 */

#include "common_ServerConfFileManager.h"
#include <sstream>

std::string ServerConfFileManager::generalNodeName = "General";
    std::string ServerConfFileManager::pathAuNodeName = "PathAU";

std::string ServerConfFileManager::conexionNodeName = "Conexion";    
    std::string ServerConfFileManager::puertoServidorNodeName = "PuertoServidor";

ServerConfFileManager::ServerConfFileManager(const std::string& filePath) {
    _confFile = new TiXmlDocument(filePath.c_str());
    _filePath = filePath;
	
    if (!_confFile->LoadFile()) {
        _CrearArchivo();
    }	
}

void ServerConfFileManager::_CrearArchivo() {
    _confFile = new TiXmlDocument();
    
    TiXmlDeclaration* decl = new TiXmlDeclaration( "1.0", "", "" );
    
	TiXmlElement* generalNode = new TiXmlElement(generalNodeName.c_str());
    TiXmlElement* pathAuNode = new TiXmlElement(pathAuNodeName.c_str());    
    generalNode->LinkEndChild(pathAuNode);
    
    TiXmlElement* conexionNode = new TiXmlElement(conexionNodeName.c_str());    
    TiXmlElement* puertoServidorNode = new TiXmlElement(puertoServidorNodeName.c_str());
    conexionNode->LinkEndChild(puertoServidorNode);    
    
    _confFile->LinkEndChild(decl);
    _confFile->LinkEndChild(generalNode);
    _confFile->LinkEndChild(conexionNode);
    
    // TODO: Deletes??
}

std::string ServerConfFileManager::GetPathAU() {
    TiXmlNode* generalNode = _confFile->FirstChild(generalNodeName.c_str());
    TiXmlNode* pathAuNode = generalNode->FirstChild(pathAuNodeName.c_str());
    TiXmlNode* innerNode = pathAuNode->FirstChild();
    
    if (innerNode) {        
        return std::string(innerNode->Value());
    } else {
        return "";
    }    
}

std::string ServerConfFileManager::GetPuertoServidor() {
    TiXmlNode* conexionNode = _confFile->FirstChild(conexionNodeName.c_str());
    TiXmlNode* puertoNode = conexionNode->FirstChild(puertoServidorNodeName.c_str());
    TiXmlNode* innerNode = puertoNode->FirstChild();
    
    if (innerNode) {
        return std::string(innerNode->Value());
    } else {
        return std::string("8080");      // DEFAULT VALUE
    }    
}

void ServerConfFileManager::SetPathAU(const std::string& pathAu) {
    TiXmlNode* generalNode = _confFile->FirstChild(generalNodeName.c_str());
    TiXmlNode* pathAuNode = generalNode->FirstChild(pathAuNodeName.c_str());
    pathAuNode->RemoveChild(pathAuNode->FirstChild());
    
    TiXmlText* nuevoPath = new TiXmlText(pathAu.c_str());
    pathAuNode->LinkEndChild(nuevoPath);    
}

void ServerConfFileManager::SetPuertoServidor(std::string& puertoServidor) {
    TiXmlNode* conexionNode = _confFile->FirstChild(conexionNodeName.c_str());
    TiXmlNode* puertoNode = conexionNode->FirstChild(puertoServidorNodeName.c_str());
    puertoNode->RemoveChild(puertoNode->FirstChild());   
    
    TiXmlText* nuevoPuerto = new TiXmlText(puertoServidor.c_str());
    puertoNode->LinkEndChild(nuevoPuerto);
}

void ServerConfFileManager::SaveChanges() {
    _confFile->SaveFile(_filePath.c_str());
}

ServerConfFileManager::~ServerConfFileManager() {    
    if (_confFile) delete _confFile;
}

